package main

import (
	"encoding/json"
	"fmt"
	"os"
)

// type ConfigManager struct {
// 	appList []AppListItem
// }

func NewConfigManager() (*ConfigManager, error) {
	cm := &ConfigManager{}

	// 读取 appList.json
	if err := cm.loadAppList(); err != nil {
		return nil, err
	}

	return cm, nil
}

func (cm *ConfigManager) loadAppList() error {
	filePath := GetConfigPath()
	fmt.Printf("Loading config from: %s\n", filePath)

	data, err := os.ReadFile(filePath)
	if err != nil {
		return fmt.Errorf("failed to read appList.json from %s: %v", filePath, err)
	}

	if err := json.Unmarshal(data, &cm.appList); err != nil {
		return fmt.Errorf("failed to parse appList.json: %v", err)
	}

	return nil
}

func (cm *ConfigManager) GetAppConfig(namespace, name, configType string) (*AppListItem, bool) {
	for _, app := range cm.appList {
		if app.Namespace == namespace && app.Name == name {
			return &app, true
		}
	}
	return nil, false
}

// 添加获取应用列表的方法
func (cm *ConfigManager) GetAppList() []AppListItem {
	return cm.appList
}
